package com.zhongwang.mms.module.material.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.material.dao.ComplexStorageDao;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.purchase.model.VWProject;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ExportExcelUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 库存综合查询 服务层
 * @Author: China.zy
 * @Date: 2020/3/21
 */
@Log4j2
@Service
public class ComplexStorageService {

    @Autowired
    private ComplexStorageDao complexStorageDao;

    @Value("${user.upload.path}")
    private String uploadPath;

    @Value("${spring.profiles.active}")
    private String active;
    @Autowired
    private LogService logService;
    /**
     * 查询原料在库明细
     *
     * @param complexStorageModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    public List<ComplexStorageModel> findStorage(ComplexStorageModel complexStorageModel, List<String> matTypeIdList, List<String> matQualityIdList, List<String> matSpecIdList, List<String> whiIdList) {

        StringBuilder sb;

        // 类型多选条件
        if (matTypeIdList != null && matTypeIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matTypeIdList.size(); i++) {
                sb.append(matTypeIdList.get(i)).append(",");
            }
            complexStorageModel.setMatTypeIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 材质多选条件
        if (matQualityIdList != null && matQualityIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matQualityIdList.size(); i++) {
                sb.append(matQualityIdList.get(i)).append(",");
            }
            complexStorageModel.setMatQualityIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 规格多选条件
        if (matSpecIdList != null && matSpecIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matSpecIdList.size(); i++) {
                sb.append(matSpecIdList.get(i)).append(",");
            }
            complexStorageModel.setMatSpecIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        if (whiIdList != null && whiIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < whiIdList.size(); i++) {
                sb.append(whiIdList.get(i)).append(",");
            }
            complexStorageModel.setWhiIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        if (complexStorageModel.getWhiIdQueryList() != null && complexStorageModel.getWhiIdQueryList().equals("''")) {
            complexStorageModel.setWhiIdQueryList(null);
        }
        if (complexStorageModel.getWhiIdListStr() != null && complexStorageModel.getWhiIdListStr().equals("")) {
            complexStorageModel.setWhiIdListStr(null);
        }
        List<ComplexStorageModel> list = null;
        try {
            list = complexStorageDao.findComplexStorage(complexStorageModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 查询原料在库明细
     *
     * @param complexStorageModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    public void CreateStorageTempTable(ComplexStorageModel complexStorageModel, List<String> matTypeIdList, List<String> matQualityIdList, List<String> matSpecIdList, List<String> whiIdList, Boolean IsHavSeeMoney) {

        StringBuilder sb;

        // 类型多选条件
        if (matTypeIdList != null && matTypeIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matTypeIdList.size(); i++) {
                sb.append(matTypeIdList.get(i)).append(",");
            }
            complexStorageModel.setMatTypeIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 材质多选条件
        if (matQualityIdList != null && matQualityIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matQualityIdList.size(); i++) {
                sb.append(matQualityIdList.get(i)).append(",");
            }
            complexStorageModel.setMatQualityIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 规格多选条件
        if (matSpecIdList != null && matSpecIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matSpecIdList.size(); i++) {
                sb.append(matSpecIdList.get(i)).append(",");
            }
            complexStorageModel.setMatSpecIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        if (whiIdList != null && whiIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < whiIdList.size(); i++) {
                sb.append(whiIdList.get(i)).append(",");
            }
            complexStorageModel.setWhiIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        if (complexStorageModel.getWhiIdQueryList() != null && complexStorageModel.getWhiIdQueryList().equals("''")) {
            complexStorageModel.setWhiIdQueryList(null);
        }
        if (complexStorageModel.getWhiIdListStr() != null && complexStorageModel.getWhiIdListStr().equals("")) {
            complexStorageModel.setWhiIdListStr(null);
        }
        if (IsHavSeeMoney) {
            complexStorageDao.createComplexStorageTempTable(complexStorageModel);
        } else {
            complexStorageDao.createComplexStorageNotMonyTempTable(complexStorageModel);
        }
    }

    /**
     * 查询原料在库明细串料专用
     *
     * @param complexStorageModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    public List<ComplexStorageModel> findChangelistStorage(ComplexStorageModel complexStorageModel, List<String> matTypeIdList, List<String> matQualityIdList, List<String> matSpecIdList, List<String> whiIdList) {

        StringBuilder sb;

        // 类型多选条件
        if (matTypeIdList != null && matTypeIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matTypeIdList.size(); i++) {
                sb.append(matTypeIdList.get(i)).append(",");
            }
            complexStorageModel.setMatTypeIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 材质多选条件
        if (matQualityIdList != null && matQualityIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matQualityIdList.size(); i++) {
                sb.append(matQualityIdList.get(i)).append(",");
            }
            complexStorageModel.setMatQualityIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 规格多选条件
        if (matSpecIdList != null && matSpecIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matSpecIdList.size(); i++) {
                sb.append(matSpecIdList.get(i)).append(",");
            }
            complexStorageModel.setMatSpecIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        if (whiIdList != null && whiIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < whiIdList.size(); i++) {
                sb.append(whiIdList.get(i)).append(",");
            }
            complexStorageModel.setWhiIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        if (complexStorageModel.getWhiIdQueryList() != null && complexStorageModel.getWhiIdQueryList().equals("''")) {
            complexStorageModel.setWhiIdQueryList(null);
        }
        if (complexStorageModel.getWhiIdListStr() != null && complexStorageModel.getWhiIdListStr().equals("")) {
            complexStorageModel.setWhiIdListStr(null);
        }
        return complexStorageDao.findChangelistComplexStorage(complexStorageModel);
    }

    /**
     * 导出原料在库明细
     *
     * @param param            查询条件
     * @param matTypeIdList    类型Id多选
     * @param matQualityIdList 材质Id多选
     * @param matSpecIdList    规格Id多选
     * @return
     */
    public JsonResult exportExcel(ComplexStorageModel param, @RequestParam(value = "matTypeIdList[]", required = false) List<String> matTypeIdList,
                                  @RequestParam(value = "matQualityIdList[]", required = false) List<String> matQualityIdList,
                                  @RequestParam(value = "matSpecIdList[]", required = false) List<String> matSpecIdList,
                                  @RequestParam(value = "whiIdList[]", required = false) List<String> whiIdList) {
        JsonResult jsonResult = new JsonResult();
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        logService.saveLog("原料库库存综合查询——导出","用户:【"+user.getName()+"】执行导出操作！");
        Subject userSubject = ShiroUtils.getSubject();
        //存储/读取Excel的文件夹的路径 mms文件夹下/用户账户/
        String path = uploadPath + user.getAccount() + "/";
        String excelFileName = "原料库库存综合查询.xlsx";
        try {
            //1.创建临时内存表名
            String tempTableName = "##" + user.getAccount() + (UUID.randomUUID().toString().replace("-", ""));
            param.setTempTableName(tempTableName);
            //2.查询出数据导入到临时表
            CreateStorageTempTable(param, matTypeIdList, matQualityIdList, matSpecIdList, whiIdList, userSubject.isPermitted("matinv:amount:view"));
            //3.导出Excel
            if (ExportExcelUtils.BuildExcel(path, excelFileName, tempTableName, active,true)) {
                jsonResult.setData(excelFileName);
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return jsonResult;
    }


    /**
     * 首页查询明细
     *
     * @param param
     * @return
     */
    public List<ComplexStorageModel> findIndexList(ComplexStorageModel param) {

        return complexStorageDao.findComplexStorage(param);
    }

    /**
     * getProjectByCode 根据项目号获取项目信息
     *
     * @param proCode 项目号
     * @return com.zhongwang.mms.module.purchase.model.VWProject
     * @throws
     * @author 徐一贺
     * @date 2020/12/7 12:09
     */
    public VWProject getProjectByCode(String proCode) {
        return complexStorageDao.getProjectByCode(proCode);
    }
}
